package com.ask.service.utilService;

import com.ask.dto.Result;
import com.ask.entity.SeckillVoucher;
import com.ask.entity.VoucherOrder;
import com.ask.service.businessService.ISecKillVoucherService;
import com.ask.service.businessService.IVoucherOrderService;
import com.ask.utils.RedisIdWorker;
import net.bytebuddy.asm.Advice;
import org.aspectj.weaver.ast.Or;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.util.concurrent.TimeUnit;

@Service
public class OrderTestService {
    @Autowired
    private IVoucherOrderService voucherOrderService;
    @Autowired
    private ISecKillVoucherService secKillVoucherService;

    @Autowired
    private RedisIdWorker redisIdWorker;

    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    public Result oneUseroneOrderTest(Long id, Long userId) {
        // 查询优惠卷库存
        String lockKey = "order_lock" + userId;

        try {
            Boolean lock = stringRedisTemplate.opsForValue().setIfAbsent(lockKey, "lock", 5, TimeUnit.SECONDS);
            if (Boolean.TRUE.equals(lock)) {
                SeckillVoucher voucher = secKillVoucherService.getById(id);
                Integer stock = voucher.getStock();
                if (stock < 1)
                    return Result.fail("库存不足！");

                // 查询用户是否重复下单
                Long count = voucherOrderService.query()
                        .eq("user_id", userId)
                        .eq("voucher_id", id)
                        .count();
                if (count > 0) return Result.fail("禁止重复下单！");

                // 扣减库存
                boolean success = secKillVoucherService.update()
                        .setSql("stock = stock - 1")
                        .eq("voucher_id", id)
                        .gt("stock", 0)
                        .update();
                if (!success)
                    return Result.fail("扣减库存失败！");

                // 新增订单
                // 创建订单
                Long orderId = redisIdWorker.nextId("order"); // 生成订单id
                // 新增订单表
                VoucherOrder order = VoucherOrder.builder()
                        .id(orderId)
                        .userId(userId)
                        .voucherId(id)
                        .build();

                voucherOrderService.save(order);


            }
            else
                return Result.fail("获取锁失败，禁止重复下单！");

        }  finally {
            // 释放锁
            stringRedisTemplate.delete(lockKey);
        }
        return Result.success("下单成功！");
    }

    public Result oneUseroneOrderTest_v2(Long id, Long userId) {
        // 库存判断
        SeckillVoucher voucher = secKillVoucherService.getById(id);
        Integer stock = voucher.getStock();
        if (stock < 1)
            return Result.fail("库存不足！");

        // 扣减库存
        boolean success = secKillVoucherService.update()
                .setSql("stock = stock - 1")
                .eq("voucher_id", id)
                .gt("stock", 0)
                .update();
        if (!success)
            return Result.fail("扣减库存失败！");

        return Result.success("扣减成功!");
    }
}
